package org.example.carsystem.config;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import lombok.extern.slf4j.Slf4j;
import org.example.carsystem.domain.SysDept;
import org.example.carsystem.mapper.DeptMapper;
import org.example.carsystem.service.DeptService;


import java.util.List;
@Slf4j
public class UserListener implements ReadListener<SysDept> {

    private static final int BATCH_COUNT = 100000;


    private List<SysDept> list = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    private DeptService deptService ;
    public UserListener(DeptService deptService){
        this.deptService = deptService;
    }
    @Override
    public void invoke(SysDept sysDept, AnalysisContext analysisContext) {
        log.info("解析到一条数据:{}", sysDept);
        list.add(sysDept);
        if(list.size() >= BATCH_COUNT){
            deptService.batchInsert(list);
            list= ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if(list.size() > 0){
            deptService.batchInsert(list);
        }
    }
}
